package com.example.asuper.check;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asuper.check.Db.CheckDAL;
import com.example.asuper.check.Helper.OperateType;
import com.example.asuper.check.Model.CheckModel;
import com.example.asuper.check.Model.TitleMessageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Overageinfo extends Activity implements View.OnClickListener, View.OnKeyListener {
    private ListView overageShowAll;
    private CheckDAL checkService;
    private List<Map<String, String>> maplist;
    private TextView tvTitleChecked;
    private TextView tvTitleUncheck;
    private TextView tvTitleOverage;
    private TextView tvTitleBack;
    private Button btSearch;
    private Button btSearchCode;
    private EditText edSearchMsg;
    private String searchType;


    private TextView tvmsg1;
    private TextView tvmsg2;
    private TextView tvmsg3;
    private TextView tvmsg4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checklist);
        setTitle("盘盈资产");
        overageShowAll = (ListView) findViewById(R.id.lv_check_showAll);
        tvTitleChecked = (TextView) findViewById(R.id.application_tv_showChecked);
        tvTitleUncheck = (TextView) findViewById(R.id.application_tv_showUncheck);
        tvTitleOverage = (TextView) findViewById(R.id.application_tv_showOverage);
        tvTitleBack = (TextView) findViewById(R.id.application_tv_titleback);
        btSearch = (Button) findViewById(R.id.bt_checklist_searchsubmit);
        edSearchMsg = (EditText) findViewById(R.id.ed_checklist_searchcontent);
        btSearchCode = (Button) findViewById(R.id.bt_checklist_searchCode);

        tvmsg1=(TextView)findViewById(R.id.tv_checklist_showmsg1);
        tvmsg2=(TextView)findViewById(R.id.tv_checklist_showmsg2);
        tvmsg3=(TextView)findViewById(R.id.tv_checklist_showmsg3);
        tvmsg4=(TextView)findViewById(R.id.tv_checklist_showmsg4);

        tvTitleBack.setOnClickListener(this);
        btSearch.setOnClickListener(this);
        btSearchCode.setOnClickListener(this);
        edSearchMsg.setOnKeyListener(this);
        initTitleMsg();
        updateDate();
        searchType = "0";
    }

    //更新查询结果
    private void updateDate() {
        checkService = new CheckDAL(this);
        try {
            overageShowAll.setAdapter(null);
            ArrayList<CheckModel> checklist = null;
            if (edSearchMsg.getText().toString().equals("")) {
                checklist = checkService.getCheckList(OperateType.STATEOVERAGE);
            } else {
                checklist = checkService.getCheckList(OperateType.STATEOVERAGE, searchType, edSearchMsg.getText().toString());
            }
            if (checklist == null || checklist.size() == 0) {
                Toast.makeText(this, "未查到任何数据！", Toast.LENGTH_SHORT).show();
                edSearchMsg.selectAll();
                return;
            }
            maplist = checkService.convertToMapList(checklist);

            tvmsg1.setText( maplist.get(0).get("column1"));
            tvmsg2.setText( maplist.get(0).get("column2"));
            tvmsg3.setText( maplist.get(0).get("column3"));
            tvmsg4.setText( maplist.get(0).get("column4"));

            SimpleAdapter adapter = new SimpleAdapter(Overageinfo.this, maplist, R.layout.layout_listhead, new String[]{"pcode", "msg1","msg2","msg3","msg4"}, new int[]{R.id.tv_checklist_showcode, R.id.tv_checklist_showmsg1, R.id.tv_checklist_showmsg2, R.id.tv_checklist_showmsg3, R.id.tv_checklist_showmsg4});

            //ArrayAdapter<CheckModel> adapter = new ArrayAdapter<CheckModel>(this, android.R.layout.simple_list_item_1, checklist);
            overageShowAll.setAdapter(adapter);
            overageShowAll.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String checkCode = maplist.get(position).get("pcode");
                    if (checkCode != null && checkCode.trim() != "") {
                        Intent intent = new Intent(Overageinfo.this, Detailinfo.class);
                        intent.putExtra("code", checkCode.trim());
                        startActivity(intent);
                    }
                }
            });
            searchType = "0";
        } catch (Exception e) {
            Toast.makeText(this, "未查到任何数据！", Toast.LENGTH_SHORT).show();
        }
        edSearchMsg.selectAll();
    }

    //初始化标题栏信息
    private void initTitleMsg() {
        checkService = new CheckDAL(this);
        TitleMessageModel titleMsg = checkService.getTitleMessage();
        if (titleMsg != null) {
            String checkedCount= (titleMsg.getCheckedCount()==null||titleMsg.getCheckedCount().toString().equals(""))?"0":titleMsg.getCheckedCount();
            String uncheckCount=(titleMsg.getUncheckCount()==null||titleMsg.getUncheckCount().toString().equals(""))?"0":titleMsg.getUncheckCount();
            String overageCount=(titleMsg.getOverageCount()==null||titleMsg.getOverageCount().toString().equals(""))?"0":titleMsg.getOverageCount();
            tvTitleChecked.setText("已盘：" + checkedCount);
            tvTitleChecked.setTextColor(Color.BLACK);
            tvTitleUncheck.setText("未盘：" + uncheckCount);
            tvTitleUncheck.setTextColor(Color.BLACK);
            tvTitleOverage.setText("盘盈：" + overageCount);
        }
    }

    //单击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.application_tv_titleback:
                finish();
                break;
            case R.id.bt_checklist_searchsubmit:
                searchType = "0";
                updateDate();
                initTitleMsg();
                break;
            case R.id.bt_checklist_searchCode:
                searchType = "1";
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, 100);
                break;
        }
    }

    //扫码结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            edSearchMsg.setText(result);
            updateDate();
        }
    }

    //回车事件
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (v.getId() == R.id.ed_checklist_searchcontent && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            searchType = "1";
            updateDate();
            initTitleMsg();
            return true;
        }
        return false;
    }
}
